$(function () {
    obj.init();

});

var obj = new function () {
    'use strict';
    return {
        dataGrid:null,
        init:function () {
            this.dataGrid = new xhDataGrid($('#list'), 'appserver/getAppBlacklist');
            this.dataGrid.parseToolbar().init().activeButton([], ['bt_list_del']);
            this.funObj().bindDataGridEvent();
            if ($('#bt_list_menu').length > 0) {
                this.funObj().initMenu();
            }
            this.listener();
        },
        listener:function () {
            var $this = this;

            //新增事件按钮
            $('#bt_list_add').on('click',function () {
                $this.funObj().openAddForm();
            });

            $('#bt_list_del').on('click',function () {
                $this.funObj().askDelQuestion();
            });

        },
        funObj:function () {
            var $this = this;
            return {
                bindDataGridEvent:function () {
                    $this.dataGrid.onDblClickRow = function (rowIndex, rowData) {

                    }
                },
                openAddForm:function () {
                    window.top.xhTop.showModal({
                        title: '新增应用黑名单',
                        content: '<iframe  src="'+baseUrl+'appserver/appBlackForm"  style="width: 100%;;border-style: none;padding: 0px;height:200px"  scrolling="yes"   ></iframe>',
                        button:'提交',
                        backClose: false
                    });
                },
                askDelQuestion:function () {
                    var id = $this.dataGrid.getSelectValue('id');
                    xhExtra.askChangeStatus('appserver/delAppBlack',{id:id},$this.dataGrid,null,function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                    });
                },
                initMenu:function () {
                    var  $btn_menu=$('#bt_list_menu').menubutton({
                        iconCls: 'fa fa-bars',
                        menu: '#mm'
                    });

                    $($btn_menu.menubutton('options').menu).menu({
                        onClick: function (item) {
                            //item 的相关属性参见API中的menu
                            var id     = $this.dataGrid.getSelectValue('id');
                            var status = $this.dataGrid.getSelectValue('status');
                            if( id  == null ){
                                window.top.xhTop.showError('错误' , '请先选择一个应用');
                                return false;
                            }
                            if (item.text == '启用') {
                                if(status == '启用'){
                                    window.top.xhTop.showError('错误' , '该配置已经启用');
                                    return false;
                                }
                                xhExtra.askChangeStatus('appserver/enableAppBlacklist',{id:id},$this.dataGrid,'确定要启用该配置项吗？',function (res) {
                                   window.top.xhTop.showSuccess(res.msg);
                                })
                            } else if(item.text == '禁用'){
                                if(status == '禁用'){
                                    window.top.xhTop.showError('错误' , '该配置已经禁用');
                                    return false;
                                }
                                xhExtra.askChangeStatus('appserver/disableAppBlacklist',{id:id},$this.dataGrid,'确定要禁用该配置项吗？',function (res) {
                                    window.top.xhTop.showSuccess(res.msg);
                                })
                            }
                        }
                    });
                }
            }
        }
    }
};